package com.campus.delivery.controller;

import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.campus.delivery.bean.CompleteOrderBean;
import com.campus.delivery.bean.ConfirmPickupBean;
import com.campus.delivery.bean.GrabOrderBean;
import com.campus.delivery.bean.order.OrderListQueryParam;
import com.campus.delivery.bean.order.CompletedOrderQueryParam;
import com.campus.delivery.service.OrderService;
import com.campus.delivery.vo.OrderVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-12
 */
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private OrderService orderService;
    
    @ApiOperation("分页查询配送订单列表")
    @GetMapping("/page")
    public Resp<Pagination<OrderVo>> page(@Validated OrderListQueryParam param) {
        PageInfo<OrderVo> pageInfo = orderService.getOrderListPage(param);
        return Resp.successPageable(pageInfo);
    }
    
    @ApiOperation("获取订单详情")
    @GetMapping("/detail/{orderId}")
    public Resp<OrderVo> getOrderDetail(@PathVariable String orderId) {
        OrderVo orderVo = orderService.getOrderDetail(orderId);
        return Resp.success(orderVo);
    }

    @ApiOperation("配送员抢单")
    @PostMapping("/grab")
    public Resp<Void> grabOrder(@RequestBody @Validated GrabOrderBean bean) {
        orderService.grabOrder(bean);
        return Resp.success();
    }

    @ApiOperation("配送员确认取货")
    @PostMapping("/confirmPickup")
    public Resp<Void> confirmPickup(@RequestBody ConfirmPickupBean bean) {
        orderService.confirmPickup(bean);
        return Resp.success();
    }

    @ApiOperation("配送员确认送达")
    @PostMapping("/complete")
    public Resp<Void> completeOrder(@RequestBody CompleteOrderBean bean) {
        orderService.completeOrder(bean);
        return Resp.success();
    }

    @ApiOperation("获取已完成的订单列表")
    @GetMapping("/completed")
    public Resp<Pagination<OrderVo>> getCompletedOrders(@Validated CompletedOrderQueryParam param) {
        PageInfo<OrderVo> pageInfo = orderService.getCompletedOrdersPage(param);
        return Resp.successPageable(pageInfo);
    }
}
